/***************************************************************
 * @System            : 生鲜系统
 * @Content           : 继承 TextEdit，增加相关通用方法或事件
 * @Author            : geping  2015-08-12
 * @Modify            : 
 * @Version           : v1.0
 * @Copyright         : 宁波康铭泰克信息科技有限公司
 ***************************************************************/

using System;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.Accessibility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;

namespace Comteck.Winforms.Controls {
  /// <summary>
  /// 
  /// </summary>
  [ToolboxItem(true)]
  public partial class MyTextEdit : TextEdit {

    #region 构造函数

    static MyTextEdit() {
      RepositoryItemMyTextEdit.Register();
    }

    /// <summary>
    /// 默认构造 
    /// </summary>
    public MyTextEdit() : base() {
      this.Tag = false;   // 设置全选标记
      //this.EnterMoveNextControl = true;   // 回车跳到下一个控件
      Enter += this.MyTextEdit_Enter;
      MouseUp += this.MyTextEdit_MouseUp;

      //if (!this.DesignMode) {
      //  this.Properties.AllowMouseWheel = false;
      //}
    }

    //
    // 摘要:
    //     Gets or sets whether focus is moved to the next control (according to the tab
    //     order) when an end-user presses ENTER.
    [DefaultValue(true)]
    //[DevExpressXtraEditorsLocalizedDescriptionAttribute("BaseEditEnterMoveNextControl")]
    [DXCategory("Behavior")]
    public override bool EnterMoveNextControl { get; set; } = true;

    #endregion

    #region fields

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => RepositoryItemMyTextEdit.EditorName;

    /// <summary>
    /// 
    /// </summary>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new RepositoryItemMyTextEdit Properties => base.Properties as RepositoryItemMyTextEdit;

    #endregion

    #region 事件

    /// <summary>
    /// 鼠标移入文本编辑框触发事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>   
    private void MyTextEdit_Enter(object sender, EventArgs e) {
      // 设置全选标记
      this.Tag = true;
      this.SelectAll();
    }

    /// <summary>
    /// 获取输入焦点时自动全选
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>    
    private void MyTextEdit_MouseUp(object sender, MouseEventArgs e) {
      // 如果鼠标左键操作并且标记存在，则执行全选
      if (e.Button == MouseButtons.Left && (bool)this.Tag) {
        this.SelectAll();
      }

      // 取消全选标记
      this.Tag = false;
    }

    #endregion
  }

  /// <summary>
  /// 
  /// </summary>
  [DXCategory("Properties")]
  [UserRepositoryItem("Register")]
  //[ToolboxItem(true)]
  public class RepositoryItemMyTextEdit : RepositoryItemTextEdit {

    /// <summary>
    /// 
    /// </summary>
    static RepositoryItemMyTextEdit() {
      Register();
    }

    /// <summary>
    /// 
    /// </summary>
    public RepositoryItemMyTextEdit() {
    }

    /// <summary>
    /// 
    /// </summary>
    internal const string EditorName = nameof(MyTextEdit);

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => EditorName;

    //
    // 摘要:
    //     Gets or sets whether value scrolling via the Mouse Wheel is supported by the
    //     editor.
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    public override bool AllowMouseWheel { get; set; } = false;

    /// <summary>
    /// 
    /// </summary>
    public static void Register() {
      EditorRegistrationInfo.Default.Editors.Add(
        new EditorClassInfo(
          EditorName, // 
          typeof(MyTextEdit),
          typeof(RepositoryItemMyTextEdit),
          typeof(DevExpress.XtraEditors.ViewInfo.TextEditViewInfo),
          new TextEditPainter(),
          true,
          null,
          typeof(TextEditAccessible)));
    }
  }

}
